package mn.slide.laborberlin.main;

import java.util.ArrayList;
import java.util.List;

import mn.slide.laborberlin.adaptor.EinExtendableAdapter;
import mn.slide.laborberlin.adaptor.SearchListAdapter;
import mn.slide.laborberlin.db.DataLayer;
import mn.slide.laborberlin.db.DatabaseManager;
import mn.slide.laborberlin.model.EmptyList;
import mn.slide.laborberlin.model.EntragData;
import mn.slide.laborberlin.tabgroup.GroupStarter;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class SearchActivity extends Activity {
	ListView marListView;
	ArrayAdapter<EntragData> adaptor;
	List<EntragData> list;

	private String[] mStrings = {};
	EditText search_edt;
	String searchTxt;
	LinearLayout homeProg;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		marListView = (ListView) findViewById(R.id.main_list);
		searchTxt = getIntent().getExtras().getString("searchTxt");
		homeProg = (LinearLayout) findViewById(R.id.homeProg);
		homeProg.setVisibility(View.VISIBLE);
		list = new ArrayList<EntragData>();
		new SearchData(SearchActivity.this, searchTxt).execute();
		DataLayer.layer.kapitelName = 1;
		search_edt = (EditText) findViewById(R.id.search_edt);
		search_edt.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// If the event is a key-down event on the "enter" button
				if ((event.getAction() == KeyEvent.ACTION_DOWN)
						&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
					String k = search_edt.getText().toString();
					if (k.length() > 0) {
						homeProg.setVisibility(View.VISIBLE);
						new SearchData(SearchActivity.this, k).execute();

					}
					return true;
				}
				return false;
			}
		});
		search_edt.setText(searchTxt);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (list.size() > 0) {
			marListView.setVisibility(View.VISIBLE);
		}
		if (adaptor != null)
			adaptor.notifyDataSetChanged();
	}

	@Override
	public void onBackPressed() {
		GroupStarter.getInstance().onBackpressed(this);
	}

	private class SearchData extends AsyncTask<Void, Void, String[]> {
		protected Activity context;
		protected SearchListAdapter adaptor;
		protected String sTxt;

		public SearchData(Activity context, String sTxt) {
			this.sTxt = sTxt;
			this.context = context;
		}

		@Override
		protected String[] doInBackground(Void... params) {

			list = DataLayer.layer.getSearchResult(searchTxt);
			if (list.size() == 0)
				list.add(new EmptyList());
			adaptor = new SearchListAdapter(context, list);

			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			super.onPostExecute(result);
			marListView.setAdapter(adaptor);
			homeProg.setVisibility(View.GONE);
		}
	}

}
